05. Input Handling + Teleportation
04 L Input Handling + Teleportation V3
INSTRUCTOR NOTE:
To find the example of teleportation in the lab, use the interaction system located within SteamVR > InteractionSystem > Teleport. You can disregard the comment about the github repo.
Note: The SteamVR plugin has been updated significantly since these videos were recorded. These updates include removing the scripts for SteamVR_TrackedObject, and SteamVR_Controller. To follow along with the next couple of lessons, you can download the version in the videos via this link.
SteamVR Plugin used in Lessons
Testing the laserMask
Now that you've watched the video and implemented and tested the code, did you notice the code for the laserMask? Are you wondering what it does? Here's how you can test it too:
- Select Cube in the hierarchy (which you might want to rename "Ground" instead).
- In the top of the Inspector click on the layer and add a new layer (should be at #8)
- Name the layer "Teleportable", meaning this will be the layer of all objects which are "teleportable", that is, you can teleport to them.
- Select Cube/Ground again and set the layer to Teleportable in the Inspector. Now it's in the layer.
- Select Controller (right) and in the Inspector, set Laser Mask to Teleportable. Now this only sees objects in this layer.
- Save scene and test in VR
- Notice how our laser locks to the ground when it intersects, but if you click off the Cube/Ground you can still change height. This shows the use of
GroundRayin the code.
Code Refactoring
Below is an alternate version of HandControllerInput.cs which has been slightly refactored. First you should first make sure the code in the video works as you expect. Next, for an extra challenge, compare the code below to what you typed in the video. It should do the same thing only written differently. Do you like the differences? Do the changes make the code better or worse? You decide!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandControllerInput : MonoBehaviour {
public SteamVR_TrackedObject trackedObject;
public SteamVR_Controller.Device device;
// Teleporter
private LineRenderer laser;
public GameObject teleportAimerObject;
public Vector3 teleportLocation;
public GameObject player;
public LayerMask laserMask;
public static float yNudgeAmount = 1f; // specific to teleportAimerObject height
private static readonly Vector3 yNudgeVector = new Vector3(0f, yNudgeAmount, 0f);
// Use this for initialization
void Start () {
trackedObject = GetComponent<SteamVR_TrackedObject>();
laser = GetComponentInChildren<LineRenderer>();
}
void setLaserStart (Vector3 startPos) {
laser.SetPosition(0, startPos);
}
void setLaserEnd (Vector3 endPos) {
laser.SetPosition(1, endPos);
}
// Update is called once per frame
void Update () {
device = SteamVR_Controller.Input((int) trackedObject.index);
if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger)) {
laser.gameObject.SetActive(true);
teleportAimerObject.SetActive(true);
setLaserStart(gameObject.transform.position);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 15, laserMask)) {
teleportLocation = hit.point;
} else {
teleportLocation = transform.position + 15 * transform.forward;
RaycastHit groundRay;
if (Physics.Raycast(teleportLocation, -Vector3.up, out groundRay, 17, laserMask)) {
teleportLocation.y = groundRay.point.y;
}
}
setLaserEnd(teleportLocation);
// aimer
teleportAimerObject.transform.position = teleportLocation + yNudgeVector;
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) {
laser.gameObject.SetActive(false);
teleportAimerObject.SetActive(false);
player.transform.position = teleportLocation;
}
}
}
(Optional) If you want to use the current version of the SteamVR Plugin…
Unity Asset Store: Current version of SteamVR Plugin
Tip: If you'd like to use the newer version of SteamVR, be sure to read the PDF files (there are multiple) that are included in the plugin after importing it into Unity and examine the sample scenes. The PDF files are the official documentation for the plugin.
You can also check out these two blog posts written by Kristin Dragos, Learning Technologist. Note that using the newer version of SteamVR means that some of the videos will not be needed, but it's still recommended to watch them. They show what's involved with setting up many mechanics from scratch.
Project Preview:
Capstone Preview:
Note: The capstone requires completing a series of "Achievements" and getting a total of 500 points in each section (Fundamentals, Completeness, and Challenges).